﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI
 * Version 1.00
 * 
 * Main Dialog Class
 *      Provides the main dialog class for the application.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Windows.Forms;

using Adaptive.Foundation.Windows.Forms;
using Adaptive.Telnet.UI.Properties;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides the main dialog class for the application.
    /// </summary>
    public partial class MainDialog : DialogBase 
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Connnection defintions.
        /// </summary>
        private ConnectionDefinitionFile _definitionFile;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public MainDialog()
            : base()
        {
            Settings appSettings = null;

            InitializeComponent();

            appSettings = new Settings();
            if (appSettings.ConnectionFile.Length == 0)
            {
                _definitionFile = new ConnectionDefinitionFile();
                appSettings.ConnectionFile = _definitionFile.PathAndFileName;
                appSettings.Save();
            }
            else
                _definitionFile = new ConnectionDefinitionFile(appSettings.ConnectionFile);

            _definitionFile.Load();

        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_definitionFile != null)
                    _definitionFile.Dispose();

                if (components != null)
                    components.Dispose();
            }

            _definitionFile = null;
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the controls on the dialog.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            //File Menu.
            FileMenu_CreateNew.Click += new EventHandler(HandleFileMenuCreateNewClick);
            FileMenu_Open.Click += new EventHandler(HandleFileMenuOpenClick);
            FileMenu_Close.Click += new EventHandler(HandleFileMenuCloseClick);
            FileMenu_CloseAll.Click += new EventHandler(HandleFileMenuCloseAllClick);
            FileMenu_Exit.Click += new EventHandler(HandleFileMenuExitClick);

            //Tools And Help Menu
            ToolsMenu_Options.Click += new EventHandler(HandleToolsOptionsClick);
            HelpMenu_About.Click += new EventHandler(HandleHelpAboutClick);

            //Toolbar.
            CreateNewButton.Click += new EventHandler(HandleFileMenuCreateNewClick);
            OpenConnectionButton.Click += new EventHandler(HandleFileMenuOpenClick);
            CloseAllButton.Click += new EventHandler(HandleFileMenuCloseAllClick);
            OptionsButton.Click += new EventHandler(HandleToolsOptionsClick);

            //Address bar.
            ConnectButton.Click +=new EventHandler(HandleConnectButtonClick);
        }
        /// <summary>
        /// Removes the event handlers for the controls on the dialog.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            //File Menu.
            FileMenu_CreateNew.Click -= new EventHandler(HandleFileMenuCreateNewClick);
            FileMenu_Open.Click -= new EventHandler(HandleFileMenuOpenClick);
            FileMenu_Close.Click -= new EventHandler(HandleFileMenuCloseClick);
            FileMenu_CloseAll.Click -= new EventHandler(HandleFileMenuCloseAllClick);
            FileMenu_Exit.Click -= new EventHandler(HandleFileMenuExitClick);

            //Tools And Help Menu
            ToolsMenu_Options.Click -= new EventHandler(HandleToolsOptionsClick);
            HelpMenu_About.Click -= new EventHandler(HandleHelpAboutClick);

            //Toolbar.
            CreateNewButton.Click -= new EventHandler(HandleFileMenuCreateNewClick);
            OpenConnectionButton.Click -= new EventHandler(HandleFileMenuOpenClick);
            CloseAllButton.Click -= new EventHandler(HandleFileMenuCloseAllClick);
            OptionsButton.Click -= new EventHandler(HandleToolsOptionsClick);

            //Address bar.
            ConnectButton.Click += new EventHandler(HandleConnectButtonClick);
        }
        /// <summary>
        /// Sets the display properties for the dialog.
        /// </summary>
        protected override void SetFormDisplay()
        {
            bool visible = (MdiChildren.Length > 0);
 
            //Menu.
            FileMenu_Close.Visible = visible;
            FileMenu_CloseAll.Visible = visible;
            CloseAllButton.Visible = visible;
            FileMenu_div1.Visible = visible;
        }
        /// <summary>
        /// Sets the initial content for the dialog.
        /// </summary>
        protected override void SetInitialContent()
        {
            
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers

        /*--------------------------------------------------------------------------------
         * File Menu Events
         *------------------------------------------------------------------------------*/
        #region File Menu Events
        /// <summary>
        /// Handles the event when the user clicks the File->New option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleFileMenuCreateNewClick(object sender, EventArgs e)
        {
            ConnectionDefinitionDialog dialog = null;       //Dialog to be displayed.
            DialogResult result = DialogResult.Cancel;      //Result of user interaction.

            Cursor = Cursors.WaitCursor;
            dialog = new ConnectionDefinitionDialog();
            result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                ConnectionDefinition newDefinition = null;

                newDefinition = new ConnectionDefinition(dialog.DefinitionName, dialog.Address, dialog.Port, dialog.Description);
                newDefinition.SendAsTyped = dialog.SendCharactersAsTyped;
                _definitionFile.Definitions.Add(newDefinition);
                _definitionFile.Save();

            }
            dialog.Dispose();
            dialog = null;
            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the File->Open option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleFileMenuOpenClick(object sender, EventArgs e)
        {
            SelectConnectionDialog dialog = null;           //Dialog to be displayed.
            DialogResult result = DialogResult.Cancel;      //Result of user interaction.

            Cursor = Cursors.WaitCursor;
            //Create and deisplay dialog.
            dialog = new SelectConnectionDialog();
            dialog.Definitions = _definitionFile.Definitions;
            result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                SessionDialog newDialog = null;

                newDialog = new SessionDialog();
                newDialog.MdiParent = this;
                newDialog.Text = "Session - [" + dialog.SelectedDefinition.Name + "]";
                newDialog.ConnectionDef = dialog.SelectedDefinition;
                newDialog.Show();
                SetFormDisplay();
            }

            dialog.Dispose();
            dialog = null;
            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the File->Close option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleFileMenuCloseClick(object sender, EventArgs e)
        {
            SessionDialog dialog = null;

            Cursor = Cursors.WaitCursor;
            dialog = (SessionDialog)ActiveMdiChild;
            if (dialog != null)
            {
                dialog.CloseConnection();
                dialog.Close();
                SetFormDisplay();
            }
            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the File->Close All option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleFileMenuCloseAllClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            foreach (DialogBase item in MdiChildren)
                ((SessionDialog)item).CloseConnection();

            while (MdiChildren.Length > 0)
                MdiChildren[0].Close();

            SetFormDisplay();
            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the File->Exit option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleFileMenuExitClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            Close();
            Cursor = Cursors.Default;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Other Menu Events
         *------------------------------------------------------------------------------*/
        #region Other Menu Events
        /// <summary>
        /// Handles the event when the user clicks the Tools->Options option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleToolsOptionsClick(object sender, EventArgs e)
        {
            ApplicationOptionsDialog dialog = null;             //Dialog to be displayed.
            DialogResult result = DialogResult.Cancel;          //Result of user interaction.

            Cursor = Cursors.WaitCursor;
            dialog = new ApplicationOptionsDialog();
            result = dialog.ShowDialog();
            if (result == DialogResult.OK)
                dialog.SaveOptions();

            dialog.Dispose();
            dialog = null;

            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the user clicks the Help->About option.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleHelpAboutClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            AboutDialog dialog = new AboutDialog();
            dialog.ShowDialog();
            dialog.Dispose();
            dialog = null;
            Cursor = Cursors.Default;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Toolbar Events
         *------------------------------------------------------------------------------*/
        #region Toolbar Events
        /// <summary>
        /// Handles the event when the user clicks the Connect button on the sub-toolbar.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleConnectButtonClick(object sender, EventArgs e)
        {
            ConnectionDefinition tempDefinition = null;
            SessionDialog dialog = null;

            Cursor = Cursors.WaitCursor;
            tempDefinition = new ConnectionDefinition("temPitem", AddressText.Text, (short)PortText.Value);
            tempDefinition.Font = AppUtilities.GetApplicationDefaultFont();
            dialog = new SessionDialog();
            dialog.Text = "Session - [" + AddressText.Text + ":" + PortText.Text + "]";
            dialog.ConnectionDef = tempDefinition;
            dialog.MdiParent = this;
            dialog.Show();
            SetFormDisplay();
            Cursor = Cursors.Default;
        }
        #endregion

        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
